package com.google.gac.masai.getit;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SpecialFolderPref extends DialogPreference {
	Context mContext;

	private SharedPreferences mPreferences;
	
	Dialog mDialog;
	
	static String rule;
	static String folderName;
	
	
	public SpecialFolderPref(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mContext = context;
		mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);

		/*
		CheckBoxPreference mCheckBoxPref = (CheckBoxPreference) findPreference("checkPref");
		PreferenceCategory mCategory = (PreferenceCategory) findPreference("category_foo");
		mCategory.removePreference(mCheckBoxPref);
*/
		// TODO Auto-generated constructor stub
		
	}
	
	protected void onClick() {
		//Dialog mine = this.getDialog();
		//mine.addContentView(view, params)
		
		
		int numberRules = mPreferences.getInt("NumSpecRules", 5);
		
		if (numberRules < 3) {
			//mContext = getContext();
			mDialog = new Dialog(mContext);
			
			mDialog.setContentView(R.layout.rule_maker);
			mDialog.setTitle("Create a special folder");
			
			
			
			
			
			Button done = (Button) mDialog.findViewById(R.id.makerule);
			Button cancel = (Button) mDialog.findViewById(R.id.cancelrule);
			
			mDialog.show();
			
			done.setOnClickListener(new OnClickListener() {
				
				//@Override
				public void onClick(View v) {
					EditText field1 = (EditText) mDialog.findViewById(R.id.spec_field1);
					EditText field2 = (EditText) mDialog.findViewById(R.id.spec_field2);
					//this isn't working for some reason
					rule = field1.getText().toString();
					folderName = field2.getText().toString();
					
					int newRuleNum = mPreferences.getInt("NumSpecRules", 0);
					boolean firstExists = mPreferences.getBoolean("first_special", true);
					boolean secondExists = mPreferences.getBoolean("second_special",true);
					boolean thirdExists = mPreferences.getBoolean("third_special", true);
					
					SharedPreferences.Editor edit = mPreferences.edit();
					
					if (!firstExists) {
						PrefsFragment.first.setTitle("Special Folder 1");
						PrefsFragment.first.setSummary("Any file containing the string \"" + rule + "\" will be placed in a folder called \"" + folderName + "\"");
						PrefsFragment.wrapper.addPreference(PrefsFragment.first);
						
						
						edit = mPreferences.edit();
						edit.putInt("NumSpecRules", ++newRuleNum);
						edit.putString("rule_one", "" + rule);
						edit.putString("folder_one", "" + folderName);
				        edit.putBoolean("first_special", true);
				        
					}
					
					else if (!secondExists) {
						PrefsFragment.second.setTitle("Special Folder 2");
						PrefsFragment.second.setSummary("Any file containing the string \"" + rule + "\" will be placed in a folder called \"" + folderName + "\"");
						PrefsFragment.wrapper.addPreference(PrefsFragment.second);
						
						
						edit = mPreferences.edit();
						edit.putInt("NumSpecRules", ++newRuleNum);
						edit.putString("rule_two", "" + rule);
						edit.putString("folder_two", "" + folderName);
				        edit.putBoolean("second_special", true);
					}
					
					else if (!thirdExists) {
						PrefsFragment.third.setTitle("Special Folder 3");
						PrefsFragment.third.setSummary("Any file containing the string \"" + rule + "\" will be placed in a folder called \"" + folderName + "\"");
						PrefsFragment.wrapper.addPreference(PrefsFragment.third);
						
						
						edit = mPreferences.edit();
						edit.putInt("NumSpecRules", ++newRuleNum);
						edit.putString("rule_three", "" + rule);
						edit.putString("folder_three", "" + folderName);
				        edit.putBoolean("third_special", true);
					}
					else {
						;
					}
					edit.commit();
					
					
					mDialog.dismiss(); // cancel dialogs
				}
			}); 
			
			cancel.setOnClickListener(new OnClickListener() {
				
				//@Override
				public void onClick(View v) {
			        mDialog.dismiss(); // close dialogs
				}
			});
		}
		else {
			//mContext = getContext();
			mDialog = new Dialog(mContext);
			mDialog.setContentView(R.layout.special_warning);
			Button warning = (Button) mDialog.findViewById(R.id.clickOK);
			
			mDialog.setTitle("Cannot make new rule");
			mDialog.show();
			
			//There is an error here
			
			warning.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
			        mDialog.dismiss(); // close dialogs
				}
			});
			
			
			
		}
		
	}
}
